package com.fh.modules.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.modules.pms.entity.PmsBrand;
import com.fh.modules.pms.service.PmsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

 /**
 * @Title: Controller
 * @Description: 品牌管理
 * @author： code generator
 * @date：   2019-06-02
 * @version： V1.0
 */
@RestController
@Slf4j
@Api("品牌管理")
public class PmsBrandController {
	@Autowired
	private PmsBrandService pmsBrandService;
	
	/**
	  * 分页列表查询
	 * @param pmsBrand
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/pms/pmsBrand/list")
	@ApiOperation("分页列表查询")
	public Result<IPage<PmsBrand>> queryPageList(PmsBrand pmsBrand,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req) {
		Result<IPage<PmsBrand>> result = new Result<IPage<PmsBrand>>();
		QueryWrapper<PmsBrand> queryWrapper = new QueryWrapper<PmsBrand>();
		QueryWrapperUtil.initQueryWrapper(queryWrapper, pmsBrand);
		Page<PmsBrand> page = new Page<PmsBrand>(pageNo, pageSize);
		IPage<PmsBrand> pageList = pmsBrandService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	
	/**
	  *   添加
	 * @param pmsBrand
	 * @return
	 */
	@PostMapping(value = "/pms/pmsBrand/add")
	@ApiOperation("添加")
	public Result<PmsBrand> add(@RequestBody PmsBrand pmsBrand) {
		Result<PmsBrand> result = new Result<PmsBrand>();
		try {
			pmsBrandService.save(pmsBrand);
			result.success("添加成功！");
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			result.error500("操作失败");
		}
		return result;
	}
	
	/**
	  *  编辑
	 * @param pmsBrand
	 * @return
	 */
	@PutMapping(value = "/pms/pmsBrand/edit")
	@ApiOperation("编辑")
	public Result<PmsBrand> edit(@RequestBody PmsBrand pmsBrand) {
		Result<PmsBrand> result = new Result<PmsBrand>();
		PmsBrand pmsBrandEntity = pmsBrandService.getById(pmsBrand.getId());
		if(pmsBrandEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = pmsBrandService.updateById(pmsBrand);
			if(ok) {
				result.success("修改成功!");
			}
		}
		
		return result;
	}
	
	/**
	  *   通过id删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/pms/pmsBrand/delete")
	@ApiOperation("通过id删除")
	public Result delete(@RequestParam(name="id",required=true) String id) {
		pmsBrandService.deleteEntityByDelFlag(id);
		return Result.ok("删除成功");
	}
	
	/**
	  *  批量删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/pms/pmsBrand/deleteBatch")
	@ApiOperation("批量删除")
	public Result deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		pmsBrandService.deleteBatchEntityByDelFlag(ids);
		return Result.ok("批量删除成功");
	}
	
	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/pms/pmsBrand/queryById")
	@ApiOperation("通过id查询")
	public Result<PmsBrand> queryById(@RequestParam(name="id",required=true) String id) {
		Result<PmsBrand> result = new Result<PmsBrand>();
		PmsBrand pmsBrand = pmsBrandService.getById(id);
		if(pmsBrand==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(pmsBrand);
			result.setSuccess(true);
		}
		return result;
	}

}
